<template>
	<div v-show="visible" :class="curClassName" :style="curStyle">
		<template v-if="mutliData && mutliData.length > 0">
			<template v-for="(data, index) of mutliData" :key="index">
				<template v-if="containerModel.length > 0">
					<div
						v-for="name of containerModel"
						class="container-item__pos"
						:style="getItemPosStyle(name)"
						:key="name"
					>
						<slot :name="name" :data="{ index }"></slot>
					</div>
				</template>
			</template>
		</template>
		<template v-else>
			<template v-if="containerModel.length > 0">
				<div
					v-for="name of containerModel"
					class="container-item__pos"
					:style="getItemPosStyle(name)"
					:key="name"
				>
					<slot :name="name"></slot>
				</div>
			</template>
		</template>
	</div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { StandardContainerProps } from './standard-container';

const props = defineProps(StandardContainerProps);

/**
 * 插槽对象
 *
 * @memberof AppStandardContainer
 */
const containerModel = computed(() => {
	let containerModel: any[] = [];
	const curLayoutModel = props.layoutModelDetails[itemName.value];
	if (
		curLayoutModel &&
		curLayoutModel.details &&
		curLayoutModel.details.length > 0
	) {
		curLayoutModel.details.forEach((key: string) => {
			containerModel.push(key);
		});
	}
	return containerModel;
});

/**
 * 项名称
 *
 * @memberof AppStandardContainer
 */
const itemName = computed(() => {
	return props.index || props.index === 0
		? `${props.name}_${props.index}`
		: props.name;
});

/**
 * 多数据容器值
 *
 * @memberof AppStandardContainer
 */
const mutliData = computed(() => {
	if (props.isMultiContainer && props.isMultiContainer === true) {
		if (props.layoutModelDetails[itemName.value]) {
			return props.layoutModelDetails[itemName.value].getData();
		}
	}
});

/**
 * 当前容器样式类
 *
 * @memberof AppStandardContainer
 */
const curClassName = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	const classNames = {
		'app-standard-container': true,
	};
	if (layoutModel) {
		Object.assign(classNames, layoutModel.getClassNames());
	}
	return classNames;
});

/**
 * 当前容器样式
 *
 * @memberof AppStandardContainer
 */
const curStyle = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	if (layoutModel) {
		return layoutModel.getElementStyle();
	}
});

/**
 * 是否显示
 *
 * @memberof AppStandardContainer
 */
const visible = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	if (layoutModel && !layoutModel.visible) {
		return false;
	}
	return true;
})

/**
 *  获取项布局样式
 */
const getItemPosStyle = (name: string) => {
	let layoutModel: any;
	if (props.index || props.index === 0) {
		layoutModel = props.layoutModelDetails[`${name}_${props.index}`];
	} else {
		layoutModel = props.layoutModelDetails[name];
	}
	if (layoutModel) {
		return layoutModel.getBoxLayOutStyle();
	}
};
</script>
